﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BoggleGUI
{
    /// <summary>
    /// Interaction logic for DisplayGrid.xaml
    /// </summary>
    public partial class DisplayGrid : UserControl
    {
        public static readonly DependencyProperty BoardLayoutProperty =
            DependencyProperty.Register("Layout", typeof(string), typeof(DisplayGrid));                
        
        public string BoardLayout 
        {
            get { return (string)this.GetValue(BoardLayoutProperty); }

            set
            {
                this.SetValue(BoardLayoutProperty, value);
                SetLayout(value);
            }
        }

        private BoggleCell[,] cells = new BoggleCell[4,4];

        public DisplayGrid()
        {
            InitializeComponent();
            InitializeGrid();
        }

        private void InitializeGrid()
        {
            for (int col = 0; col < 4; col++)
            {
                for (int row = 0; row < 4; row++)
                {
                    // create a new boggle cell and add it to the collection
                    BoggleCell cell = new BoggleCell(col, row);
                    cells[col, row] = cell;

                    // set the label to ' '
                    cell.SetLabel(' ');

                    // position the cell on the canvas
                    Canvas.SetLeft(cell, col * 75);
                    Canvas.SetTop(cell, row * 75);

                    // make the cell visible
                    GridCanvas.Children.Add(cell);
                }
            }
        }

        private void SetLayout(string s)
        {
            int index;

            for (int col = 0; col < 4; col++)
            {
                for (int row = 0; row < 4; row++)
                {
                    index = col + row * 4;

                    cells[col, row].SetLabel(s[index]);
                }
            }
        }

        public void ClearSelection()
        {
            foreach (BoggleCell c in cells)
            {
                c.ResetPiece();
            }
        }

        private void GridCanvas_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            ClearSelection();
        }
    }
}
